home *** CD-ROM | disk | FTP | other *** search
/ Programming Microsoft Visual Basic .NET / Programming Microsoft Visual Basic .NET (Microsoft Press)(X08-78517)(2002).bin / 07 attributes / attributesdemo / module1.vb < prev   
Text File  |  2002-03-16  |  7KB  |  190 lines

  1. Imports System.Runtime.InteropServices
  2.  
  3. Module MainModule
  4.  
  5.     Sub Main()
  6.         ' Run one of the Textxxxx procedures below by uncommenting only one statement
  7.  
  8.         'TestFieldOffset()
  9.         'TestUnions()
  10.         'TestDllImport()
  11.         'TestConditionalAttribute()
  12.         'TestObsoleteAttribute()
  13.         'TestListVersioningAttributes()
  14.  
  15.         ' These statements are usuful when running inside Visual Studio.NET
  16.         Console.WriteLine("")
  17.         Console.WriteLine(">>> write Enter to terminate the program <<<")
  18.         Console.ReadLine()
  19.     End Sub
  20.  
  21.     ' This procedure tests the FieldOffset attributes
  22.     Sub TestFieldOffset()
  23.         ' Split a color into its components.
  24.         Dim rgb As ARGBColor
  25.         rgb.Value = &H112233         ' This is equal to 1122867.
  26.         Console.WriteLine(String.Format("Red={0}, Green={1}, Blue={2}", _
  27.             rgb.Red, rgb.Green, rgb.Blue))
  28.         ' => Red=51, Green=34, Blue=17
  29.     End Sub
  30.  
  31.     ' this procedure tests "unions"
  32.  
  33.     Sub TestUnions()
  34.         Dim it As IntegerTypes
  35.  
  36.         it.Short0 = 517                  ' hex 0205
  37.         Console.WriteLine(it.Byte0)        ' => 5
  38.         Console.WriteLine(it.Byte1)        ' => 2
  39.  
  40.         Console.WriteLine(it.LowByte(517))           ' => 5
  41.         Console.WriteLine(it.HighByte(517))          ' => 2
  42.         Console.WriteLine(it.LowWord(&HFFFF1000))    ' => 4096
  43.         Console.WriteLine(it.HighWord(&HFFFF1000))   ' => -1
  44.     End Sub
  45.  
  46.     ' Note that this code works even though the actual procedure
  47.     ' is named FindWindowA, because the compiler tracks it automatically.
  48.     <DllImport("user32")> _
  49.     Function FindWindow _
  50.         (ByVal lpClassName As String, ByVal lpWindowName As String) As Integer
  51.         ' no implementation code    
  52.     End Function
  53.  
  54.     <DllImport("user32")> _
  55.     Function MoveWindow _
  56.         (ByVal hWnd As Integer, ByVal x As Integer, ByVal y As Integer, _
  57.         ByVal nWidth As Integer, ByVal nHeight As Integer, _
  58.         ByVal bRepaint As Integer) As Integer
  59.         ' no implementation code 
  60.     End Function
  61.  
  62.     ' this procedure tests the DllImport attribute
  63.  
  64.     Sub TestDllImport()
  65.         ' NOTE: launch Notepad on an empty document before running this code.
  66.         Dim hWnd As Integer = FindWindow(Nothing, "Untitled - Notepad")
  67.         ' If found, resize it and move it to upper-left corner.
  68.         If hWnd <> 0 Then MoveWindow(hWnd, 0, 0, 600, 300, 1)
  69.     End Sub
  70.  
  71.     ' this procedure tests the Conditional attribute
  72.  
  73.     Sub TestConditionalAttribute()
  74.         LogMsg("Program is starting")
  75.         ' ..
  76.         LogMsg("Program is ending")
  77.     End Sub
  78.  
  79.     ' Goto to the Configuration Properties | Build to set the LOG constant
  80.     ' to a non-zero value to include this procedure
  81.  
  82.     <Conditional("LOG")> _
  83.     Sub LogMsg(ByRef MsgText As String)
  84.         Console.WriteLine(MsgText)
  85.     End Sub
  86.  
  87.     ' this procedure tests the Obsolete attribute
  88.  
  89.     Sub TestObsoleteAttribute()
  90.         Dim arr() As String
  91.         ' next line is flagged as a warning
  92.         BubbleSort(arr)
  93.  
  94.         ' *** uncomment next line to see a compile error
  95.         ' BubbleSort2(arr)
  96.     End Sub
  97.  
  98.     ' Mark the BubbleSort as obsolete - this causes a compiler warning
  99.     <Obsolete("Replace BubbleSort with ShellSort")> _
  100.     Sub BubbleSort(ByVal arr() As String)
  101.         ' ...
  102.     End Sub
  103.  
  104.     ' Mark the BubbleSort2 as obsolete - this causes a compiler error
  105.     <Obsolete("Replace BubbleSort with ShellSort", True)> _
  106.     Sub BubbleSort2(ByVal arr() As String)
  107.         ' ...
  108.     End Sub
  109.  
  110.     ' this procedure lists the Versioning attribute
  111.     Sub TestListVersioningAttributes()
  112.         Dim attributes() As Object
  113.         Dim att As VersioningAttribute
  114.         Dim method As System.Reflection.MethodInfo
  115.         Dim attType As Type = GetType(VersioningAttribute)
  116.         Dim classType As Type = GetType(TestClass)
  117.  
  118. #Const ASSUME_MULTIPLE_OCCURRENCES = True
  119.  
  120. #If ASSUME_MULTIPLE_OCCURRENCES Then
  121.         ' this version shows how to query for custom attributes that
  122.         ' can have multiple occurrences
  123.  
  124.         ' Retrieve all the custom attributes of type VersioningAttribute.
  125.         attributes = Attribute.GetCustomAttributes(classType, attType)
  126.  
  127.         ' Check whether the array contains any element (it should contain one).
  128.         If attributes.Length > 0 Then
  129.             ' Move to a specific type variable so we can use early binding.
  130.             att = DirectCast(attributes(0), VersioningAttribute)
  131.             ' Display versioning information on the TestClass itself.
  132.             Console.WriteLine("Class TestClass")
  133.             Console.WriteLine("  Author = " & att.Author)
  134.             Console.WriteLine("  Version = " & att.Version.ToString)
  135.             Console.WriteLine("  Tested = " & att.Tested.ToString)
  136.         End If
  137.  
  138.         ' Iterate over all the methods in TestClass.
  139.         For Each method In classType.GetMethods
  140.             ' Get Versioning attributes for this method.
  141.             attributes = Attribute.GetCustomAttributes(method, attType)
  142.             ' If there are custom Versioning attributes.
  143.             If attributes.Length > 0 Then
  144.                 ' Display the name of this method.
  145.                 Console.WriteLine("Method " & method.Name)
  146.                 ' Get a Versioning object to use early binding.
  147.                 att = DirectCast(attributes(0), VersioningAttribute)
  148.                 ' Display versioning information on this method.
  149.                 Console.WriteLine("  Author = " & att.Author)
  150.                 Console.WriteLine("  Version = " & att.Version.ToString)
  151.                 Console.WriteLine("  Tested = " & att.Tested.ToString)
  152.             End If
  153.         Next
  154. #Else
  155.  
  156.         ' this simpler version assumes that the attribute has no 
  157.         ' multiple occurrences.
  158.  
  159.         ' Retrieve the (only) VersioningAttribute, or Nothing.
  160.         att = DirectCast(Attribute.GetCustomAttribute(classType, attType), VersioningAttribute)
  161.  
  162.         ' Check whether the attribute was found.
  163.         If Not (att Is Nothing) Then
  164.             ' Display versioning information on the TestClass itself.
  165.             Console.WriteLine("Class TestClass")
  166.             Console.WriteLine("  Author = " & att.Author)
  167.             Console.WriteLine("  Version = " & att.Version.ToString)
  168.             Console.WriteLine("  Tested = " & att.Tested.ToString)
  169.         End If
  170.  
  171.         ' Iterate over all the methods in TestClass.
  172.         For Each method In classType.GetMethods
  173.             ' Get the Versioning attribute for this method.
  174.             att = DirectCast(Attribute.GetCustomAttribute(method, attType), VersioningAttribute)
  175.             ' If there are custom Versioning attributes.
  176.             If Not (att Is Nothing) Then
  177.                 ' Display the name of this method.
  178.                 Console.WriteLine("Method " & method.Name)
  179.                 ' Display versioning information on this method.
  180.                 Console.WriteLine("  Author = " & att.Author)
  181.                 Console.WriteLine("  Version = " & att.Version.ToString)
  182.                 Console.WriteLine("  Tested = " & att.Tested.ToString)
  183.             End If
  184.         Next
  185. #End If
  186.  
  187.     End Sub
  188.  
  189. End Module
  190.